<?php
namespace Api\Controller;
use Think\Controller;
class IndexController extends Controller {

    /*登陆  开发人：周弈男*/
    public function login(){
       $user=M('user');
       $phone=$_GET['username'];
       $pwd=$_GET['pwd'];
       $map = array();
       $map['phone'] = trim($phone);
       $map['pwd'] = md5(trim($pwd));
       $user=$user->where($map)->find();
       if($user==null){
          $re['userid']='0';
          $this->ajaxReturn($re);
       }else{
          $re['userid']=$user['userid'];
          $this->ajaxReturn($re);
       }
    }
    /*注册  开发人：周弈男*/
    public function reg(){
      $user=M('user');
      $phone =$_GET['phone'];
      $username = $_GET['username'];
      $pwd = $_GET['pwd'];

      if ($_FILES['pic']['error'] == 0) {
        $info = $this->upload();
        $name=$info['pic']['savename'];
      } else {
        $name = "2.jpg";
      }

      $data['username']=trim($username);
      $data['userphone']=trim($phone);
      $data['pwd']=md5(trim($pwd));
      $data['state'] ="1";
      $data['pic']=$name;
      $user=$user->add($data);
      if($user!==false){
          $re['success']='1';
          $this->ajaxReturn($re);
      }else{
          $re['success']='0';
          $this->ajaxReturn($re);
      }
    }

    public function exist(){
      $user=M('user');
      $phone =$_GET['phone'];
      $user=$user->where("userphone=$phone")->find();
      if($user==false){
          $re['success']='0';
          $this->ajaxReturn($re);
      }else{
          $re['success']='1';
          $this->ajaxReturn($re);
      }
    }

    public function userinfo(){
      $user=M('user');
      $id= $_GET['uid'];
      $user=$user->where("userid=$id")->find();
      if($user==null){
          $this->ajaxReturn(null);
       }else{
          $this->ajaxReturn($user);
       }
    }

    public function serach(){
      $content=$_GET['content'];

      $activity = M('activity');
      $club = M('club');
      $train = M('train');
      $game = M('game');

      $map1['activityname']=array('like',"%$content%");
      $map2['clubname']=array('like',"%$content%");
      $map3['trainname']=array('like',"%$content%");
      $map4['gamename']=array('like',"%$content%");

      $activity = $activity->where($map1)->field('activityid,activityname')->select();
      $club = $club->where($map2)->field('clubid,clubname')->select();
      $train = $train->where($map3)->field('trainid,trainname')->select();
      $game = $game->where($map4)->field('gameid,gamename,web')->select();

      $serach = array_merge($activity,$club,$train,$game);

      if($serach==null){
        $this->ajaxReturn($serach);
      }else{
        $this->ajaxReturn($serach);
      }
    }

    public function upload(){
        $upload = new \Think\Upload();
        $upload->maxSize = 3145728 ;
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');
        $upload->rootPath = './Public/photo/';
        $upload->autoSub=false;
        $info = $upload->upload();

        if(!$info) {        
            $this->error($upload->getError());    
        }else{        
           return $info; 
        }                    
    }
}
